#!/bin/bash

# Script to create a bare git repository and set up git daemon remote
# Only includes files from the current directory (not parent git repositories)
# Usage: ./addgit.sh <repository_name>

set -e  # Exit on any error

# Configuration
GIT_SERVER_BASE="/home/linden/code/gitee/git-server"
GIT_DAEMON_HOST="127.0.0.1"
GIT_DAEMON_PORT="9418"

# Input validation
if [ $# -eq 0 ]; then
    echo "Error: Repository name is required"
    echo "Usage: $0 <repository_name>"
    exit 1
fi

file="$1"

# Validate repository name (no special characters, spaces, etc.)
if [[ ! "$file" =~ ^[a-zA-Z0-9_-]+$ ]]; then
    echo "Error: Repository name can only contain letters, numbers, underscores, and hyphens"
    exit 1
fi

echo "Creating bare repository: $file"

# Create the bare repository
repo_path="$GIT_SERVER_BASE/$file"
if [ -d "$repo_path" ]; then
    echo "Warning: Repository directory already exists at $repo_path"
else
    git init --bare "$repo_path"
    echo "✓ Created bare repository at $repo_path"
fi

# Create git-daemon-export-ok file to allow git daemon access
touch "$repo_path/git-daemon-export-ok"
echo "✓ Created git-daemon-export-ok file"

# Always create a fresh git repository in current directory (ignore parent git repos)
echo ""
echo "Creating fresh git repository for current directory only..."

# Remove any existing .git directory to ensure fresh start
if [ -d ".git" ]; then
    echo "Removing existing .git directory to create fresh repository..."
    rm -rf .git
fi

# Initialize new git repository
git init
echo "✓ Initialized fresh git repository"

# Check if there are any files to add (exclude hidden files except .gitignore)
if [ -n "$(find . -maxdepth 1 -type f -not -path './.*' -print -quit)" ] || [ -f ".gitignore" ]; then
    echo "Staging all files in current directory..."
    
    # Add all visible files and .gitignore if it exists
    git add .
    
    # Make initial commit
    git commit -m "Initial commit - added by addgit.sh script"
    echo "✓ Made initial commit with current directory files"
else
    echo "⚠ No files found to commit. Directory is empty."
    exit 1
fi

# Set up the remote
remote_url="git://$GIT_DAEMON_HOST:$GIT_DAEMON_PORT/$file"
git remote add local "$remote_url"
echo "✓ Set up remote 'local' -> $remote_url"

# Get current branch name
current_branch=$(git branch --show-current)

if [ -n "$current_branch" ]; then
    echo ""
    echo "Pushing current branch '$current_branch' to remote..."
    
    # Push and set upstream tracking
    if git push -u local "$current_branch"; then
        echo "✓ Successfully pushed '$current_branch' to remote 'local'"
        # Set the default branch in the bare repository to match the pushed branch
        git --git-dir="$repo_path" symbolic-ref HEAD "refs/heads/$current_branch"
        echo "✓ Set default branch to '$current_branch' in bare repository"
    else
        echo "⚠ Failed to push to remote. You can push manually with: git push -u local $current_branch"
    fi
else
    echo ""
    echo "⚠ No current branch detected. You can push manually with: git push -u local <branch_name>"
fi

echo "Done!"
echo " You can clone the repository with:"
echo "git clone git://127.0.0.1:9418/$file"
echo "If you are on remote server, you can clone the repository with:"
echo "git clone git://127.0.0.1:1234/$file" #where 1234 is the port of the remote server which bind to local 9418 port.
